﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccessLayer
{
    public class AdjustmentRepository
    {
        StationeryInventoryEntities1 ent;

        public AdjustmentRepository()
        {
            ent = new StationeryInventoryEntities1();
        }

        #region Select Method

        public List<Adjustment> GetAllPendingAdjustmentForSupervisor()
        {
            List<Adjustment> list = new List<Adjustment>();

            var adj = ent.Adjustments.Where(x => x.Status.Equals("Supervisor"));

            if (adj != null)
            {
                list = adj.ToList();
            }
            return list;
        }

        public List<Adjustment> GetAllPendingAdjustmentForStoreManager()
        {
            List<Adjustment> list = new List<Adjustment>();

            var adj = ent.Adjustments.Where(x => x.Status.Equals("Manager"));

            if (adj != null)
            {
                list = adj.ToList();
            }
            return list;
        }

        public Adjustment GetAdjustmentByAdjVoucherId(int voucherId)
        {
            Adjustment adjustment = new Adjustment();

            var adj = ent.Adjustments.FirstOrDefault(x => x.AdjVoucherID == voucherId);

            if (adj != null)
            {
                adjustment = adj;
            }
            return adjustment;
        }

        #endregion

        #region Create Method

        public void CreateAdjustment(Adjustment adjustment)
        {
            ent.Adjustments.Add(adjustment);
            ent.SaveChanges();
        }

        #endregion

        #region Update Method

        public void UpdateAdjustment(Adjustment adjustment)
        {
            var adj = GetAdjustmentByAdjVoucherId(adjustment.AdjVoucherID);

            adj.RequestedBy = adjustment.RequestedBy;
            adj.RequestedDate = adjustment.RequestedDate;
            adj.ApprovalBy = adjustment.ApprovalBy;
            adj.ApprovalDate = adjustment.ApprovalDate;
            adj.Status = adjustment.Status;

            ent.SaveChanges();
        }

        #endregion
    }
}
